package game.ioc.shop;

import game.memc.base.shop.FavoriteGoodsDB;
import game.memc.base.shop.GoodsDB;
import game.util.conf.ShopStatic;

import java.util.ArrayList;
import java.util.List;

import com.cs.city.bean.base.human.PlayerInfo;
import com.cs.city.bean.base.shop.Goods;

/**
 * 
 * @author Linzy
 * Create Date: 2010 3 19 09:59:30
 */
public class GoodsHivImp implements GoodsHiv {


	/* (non-Javadoc)
	 * @see game.ioc.shop.GoodsHiv#incBuyCount(long, int)
	 */
	public void updateBuyCount(long goodsId, int quantity) {
		GoodsDB.updateBuyCount(goodsId, quantity);
	} // updateBuyCount()

	/*
	 * (non-Javadoc)
	 * @see game.ioc.shop.GoodsHiv#addFavor(long, long, long)
	 */
	public boolean addFavor(long userId, long playerId, long goodsId) {
		List<Long> gIdLst = getFavorLst(userId, playerId);
		if(gIdLst != null){
			if(gIdLst.contains(Long.valueOf(goodsId))){
				return false;
			}else if(gIdLst.size() == ShopStatic.favorLimit){
				if(!FavoriteGoodsDB.deleteOneBean(userId, playerId))
					return false;
					
			}
		}
		
		if(!FavoriteGoodsDB.addFavor(userId, playerId, goodsId)){
			return false;
		}
		return FavoriteGoodsDB.initFavorIdLst(userId, playerId);
	} // addFavor()

	/*
	 * (non-Javadoc)
	 * @see game.ioc.shop.GoodsHiv#getFavorGoodsLst(com.wr.xb.bean.base.human.PlayerInfo)
	 */
	public List<Goods> getFavorGoodsLst(PlayerInfo player) {
		List<Long> gIdLst = getFavorLst(player.getUserId(), player.getPlayerId());
		if(gIdLst == null || gIdLst.size() <= 0){
			return null;
		}
		List<Goods> retLst = new ArrayList<Goods>();
		for(Long gId : gIdLst){
			Goods goods = getGoods(gId);
			if(goods != null){
				retLst.add(goods);
			}
		}
		return retLst;
	}
	
	/*
	 * (non-Javadoc)
	 * @see game.ioc.shop.GoodsHiv#getFavorLst(long, long)
	 */
	public List<Long> getFavorLst(long userId, long playerId) {
		return FavoriteGoodsDB.getFavorIdLst(userId, playerId);
	}

	/*
	 * (non-Javadoc)
	 * @see game.ioc.shop.GoodsHiv#getGoods(long)
	 */
	public Goods getGoods(long goodsId) {
		return GoodsDB.getBean(goodsId);
	}

	/*
	 * (non-Javadoc)
	 * @see game.ioc.shop.GoodsHiv#getGoodsLst(java.lang.String)
	 */
	public List<Goods> getGoodsLst(String goodsType) {
		return GoodsDB.getBeanLst(goodsType);
	} // getGoodsLst()

	/*
	 * (non-Javadoc)
	 * @see game.ioc.shop.GoodsHiv#getHotLst()
	 */
	public List<Goods> getHotLst() {
		return GoodsDB.getTopSale(ShopStatic.topNum);
	}

	/* (non-Javadoc)
	 * @see game.ioc.shop.GoodsHiv#initHotLst()
	 */
	public boolean initHotLst() {
		if(!GoodsDB.clearHotCache()){
			return false;
		}
		
		getHotLst();
		return true;
	}

	/* (non-Javadoc)
	 * @see game.ioc.shop.GoodsHiv#search(java.lang.String)
	 */
	public List<Goods> search(String keyWord) {
		return GoodsDB.search(keyWord);
	}

	
	
} // end of class
